// Day17手写：
// 1.手写算法：https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */

/**
 * Encodes a tree to a single string.
 *
 * @param {TreeNode} root
 * @return {string}
 */
 var serialize = function (root) {
    let list = []
    function read(root) {
        if (!root) {
            list.push('null')
        } else {
            list.push(root.val)
            read(root.left)
            read(root.right)
        }
    };
    read(root)
    return list.join(',');

};

/**
 * Decodes your encoded data to tree.
 *
 * @param {string} data
 * @return {TreeNode}
 */
var deserialize = function (data) {
    const list = data.split(',')
    function write(list) {
        if (list[0] === "null") {
            list.shift();
            return null;
        }
        const root = new TreeNode(parseInt(list[0]));
        list.shift();
        root.left = write(list);
        root.right = write(list);

        return root;
    }
    return write(list)
};

/**
 * Your functions will be called as such:
 * deserialize(serialize(root));
 */

// 2.手写题
//   题目描述:渲染百万条结构简单的大数据时 怎么使用分片思想优化渲染
